package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.text.TextUtils
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.starsea.book.R
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemNewUserPopBinding
import com.starsea.book.ui.newpage.entity.Today
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class NewUserAdapter(
    context: Context,
    var onChanged: ((value: Today) -> Unit)? = null
) :
    RecyclerAdapter<Today, ItemNewUserPopBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemNewUserPopBinding {
        return ItemNewUserPopBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemNewUserPopBinding) {

    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemNewUserPopBinding,
        item: Today,
        payloads: MutableList<Any>
    ) {
        binding.run {
//            tvTitle.text = item.time
//            奖励类型 1、金币 2、现金，3、答题卡 4.抽奖次数
            if (item.amountType == 1) {
                ivImage.setImageResource(R.mipmap.icon_new_user_coin)
                rtvNumber.text = (item.amount ?: "0").toDouble().finialDouble()
            } else {
                ivImage.setImageResource(R.mipmap.icon_new_user_red_bag)
                rtvNumber.text = "${(item.amount ?: "0").toDouble().finialDouble()}元"
            }

            rtvDay.text = "第${item.time ?: "0"}天"

            if (TextUtils.isEmpty(item.recordId)) {
                rllBg.isSelected = false
            } else {
                rllBg.isSelected = true
            }


        }
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {

        super.onAttachedToRecyclerView(recyclerView)


        val layoutManager = recyclerView.layoutManager

        if (layoutManager is GridLayoutManager) {
            val gridLayoutManager: GridLayoutManager = layoutManager

            gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return if (position == itemCount - 1) 2 else 1
                }

            }

        }

    }


}